# Figure A1: Result of the hate crimes classification process

rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(ggplot2)

crimes <- fread('./data/2007_2020_hate_crimes_classified.csv')
crimes[region_broad!='',label:='region of origin']
crimes[immigrant!='' & region_broad=='', label:='immigrant']
crimes[(religion_broad!='' | fascism!='') & (immigrant=='' & region_broad==''), label:='religion']
crimes[is.na(label), label:='other']

crimes[label=='region of origin', label:=paste('immigrant','known origin',sep="\n")]
crimes[label=='immigrant', label:=paste('immigrant','unknown origin',sep="\n")]
crimes[label=='religion', label:=paste('ethnic minority','religion',sep="\n")]
crimes[label=='other', label:=paste('not','classified',sep="\n")]
crimes$label <- factor(crimes$label, levels = c(paste('immigrant','known origin',sep="\n"), paste('immigrant','unknown origin',sep="\n"),
                                                paste('ethnic minority','religion',sep="\n"), paste('not','classified',sep="\n")))
ggplot(crimes, aes(label)) +
  geom_bar() +
  geom_text(aes(label = scales::percent((..count..)/sum(..count..))),,stat ='count',
            vjust = -.5) +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1.2)), axis.text.x=element_text(size=12), axis.text.y=element_text(size=12)) +
  xlab(NULL) + ylab('Hate crimes')
ggsave('./count_crimes_classification.pdf', width=6, height=4.5)

# Figure A2: Proportion of classified crimes against ethnic minorities and immigrant groups, and with known victim's region of origin
# Figure A2 (b)
crimes[,date:=as.Date(crimes$Date, '%d-%m-%Y')]
crimes[,month:=as.Date(cut(date,breaks = "month", start.on.monday = FALSE))]
crimes[region_broad!='',labeled:=.N, by=month]
crimes[is.na(labeled),labeled:=0]
crimes[,total:=.N, by=month]
prop_labeled <- unique(crimes[,labeled/total,by=month])

ggplot(prop_labeled, aes(month, V1)) +
  geom_line()  +
  scale_x_date(breaks = "6 month", minor_breaks = "1 month", date_labels = '%Y-%m',
               limits = c(min(prop_labeled$month), max = max(prop_labeled$month)),
               expand=c(0,0)) + xlab(NULL) +
  ylab("proportion of crimes with assigned victim's origin") +
  geom_vline(xintercept = as.numeric(as.Date('01-01-2020','%d-%m-%Y')), color='blue', linetype='dashed') +
  geom_text(x=as.numeric(as.Date('01-01-2019','%d-%m-%Y')), y=0.99, label="Covid-19", color='blue') +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position ='none',
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(0.9)), axis.title.x = element_text(size = rel(1.1)))
ggsave('./prop_labeled_origin.pdf', width=6, height=4.5)

# Figure A2 (a)
crimes <- fread('./data/2007_2020_hate_crimes_classified.csv')
crimes[,date:=as.Date(crimes$Date, '%d-%m-%Y')]
crimes[,month:=as.Date(cut(date,breaks = "month", start.on.monday = FALSE))]
crimes[region_broad!='',label:='region of origin']
crimes[immigrant!='' & region_broad=='', label:='immigrant']
crimes[(religion_broad!='' | fascism!='') & (region_broad=='' & region_broad==''), label:='religion']
crimes[is.na(label), label:='other']
crimes[label!='other',classified:=.N, by=month]
crimes[is.na(classified),classified:=0]
crimes[,total:=.N, by=month]
prop_classified <- unique(crimes[,classified/total,by=month])

ggplot(prop_classified, aes(month, V1)) +
  geom_line()  +
  scale_x_date(breaks = "6 month", minor_breaks = "1 month", date_labels = '%Y-%m',
               limits = c(min(prop_classified$month), max = max(prop_classified$month)),
               expand=c(0,0)) + xlab(NULL) +
  ylab("proportion of crimes against ethnic minorities and immigrants") +
  geom_vline(xintercept = as.numeric(as.Date('01-01-2020','%d-%m-%Y')), color='blue', linetype='dashed') +
  geom_text(x=as.numeric(as.Date('01-01-2019','%d-%m-%Y')), y=0.99, label="Covid-19", color='blue') +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position ='none',
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(0.9)), axis.title.x = element_text(size = rel(1.1)))
ggsave('./prop_classified_immigrant.pdf', width=6, height=4.5)

